package com.lagou.runnable;

import com.lagou.service.UserService;
import com.lagou.service.com.lagou.zookeeper.ZooKeeperServiceDiscovery;


public class ResponseThread extends Thread {

    @Override
    public void run() {
        try {
            while (true) {
                Thread.sleep(5000);
                handleResponseInfo();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void handleResponseInfo(){
        ZooKeeperServiceDiscovery discovery = new ZooKeeperServiceDiscovery("127.0.0.1");
        String serviceResponseInfo = discovery.getServiceResponseInfo(UserService.class.getName(), "127.0.0.1:8992");
        if(serviceResponseInfo != null){
            String[] Arrays =  serviceResponseInfo.split(":");
            if(Arrays != null && Arrays.length==2){
                long responseTime = Long.parseLong(Arrays[1]);
                long time = System.currentTimeMillis();
                long differenceTime = time - responseTime;
                if(differenceTime > 5000){
                    discovery.deleteNode(UserService.class.getName(), "127.0.0.1:8992");
                    System.out.println("------------------>serviceResponseInfo 超出5秒钟已删除");
                }else{
                    System.out.println("------------------>serviceResponseInfo 未超出5秒钟！！！");
                }

            }
        }else{
            System.out.println("------------------>serviceResponseInfo 暂无数据");
        }
    }
}